package com.example.adapters;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.entities.MP3;
import com.example.musicplayer01.MainActivity;
import com.example.musicplayer01.R;
import com.example.services.MusicService;
import com.example.utils.MusicUtils;

import java.util.List;

public class MusicAdapterStyleOne extends RecyclerView.Adapter<MusicAdapterStyleOne.InnerHolder> {
    private List<MP3> data;
    private Context context;

    public static int[] pictures={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8};

    public MusicAdapterStyleOne(List<MP3> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context=parent.getContext();//获取父级的环境

        View view=LayoutInflater.from(context).inflate(R.layout.music_list_item,parent,false);//设置视图
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(data.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicService.index=position;
                Message msg=new Message();
                msg.what=200;
                MainActivity.handler.sendMessage(msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class InnerHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView title;
        TextView musician;
        TextView duration;

        InnerHolder(@NonNull View itemView) {
            super(itemView);
            initItemView();
        }

        void setData(MP3 mp3){
            imageView.setImageResource(pictures[mp3.getId()%8]);
            title.setText(mp3.getTitle());
            musician.setText(mp3.getMusician());
            duration.setText(MusicUtils.timeParse(mp3.getDuration()));
        }

        private void initItemView(){
            imageView=itemView.findViewById(R.id.musicImg);
            title=itemView.findViewById(R.id.musicName);
            musician=itemView.findViewById(R.id.musician);
            duration=itemView.findViewById(R.id.maxTime);
        }
    }

}
